import api from "@/api/apiConfig";
import ajax from "@/api/http";
import store from "@/store";
import router from "@/router";
import { message } from "ant-design-vue";

export default {
  goPage(routerName, params, query) {
    router.push({
      name: routerName,
      params: { id: params },
      query: { info: query }
    });
  },
  getInfo(cb) {
    if (localStorage.getItem("mtoken")) {
      ajax
        .post(api.getPerson)
        .then(res => {
          store.dispatch("changeMobile", res.data.mobile);
          store.dispatch("changeLoginStatus", true);
          store.dispatch("changeValidate", res.data.is_check);
          store.dispatch("changeMobileStatus", res.data.safeMobile);
          store.dispatch("changeSetPay", res.data.pay_password);
          store.dispatch("changeSign", res.data.is_sign);
          store.dispatch("changeAccount", res.data.username);
          cb && cb();
        })
        .catch(err => {
          message.error(err);
          cb && cb();
        });
    } else {
      store.dispatch("resetState");
      cb && cb();
    }
  },
  // 是否为pc端
  // isPC() {
  //   let flag = true;
  //   let userAgentInfo = navigator.userAgent;
  //   let Agents = [
  //     "Android",
  //     "iPhone",
  //     "SymbianOS",
  //     "Windows Phone",
  //     "iPad",
  //     "iPod"
  //   ];
  //   for (var v = 0; v < Agents.length; v++) {
  //     if (userAgentInfo.indexOf(Agents[v]) > 0) {
  //       flag = false;
  //       break;
  //     }
  //   }
  //   let state = sessionStorage.getItem("state");
  //   if (state) {
  //     store.replaceState(JSON.parse(state));
  //   }
  //   store.dispatch("changeIsPC", flag);
  // },
  logout(routerName) {
    ajax
      .post(api.logout)
      .then(() => {
        localStorage.removeItem("mtoken");
        sessionStorage.clear();
        router.push({ name: routerName });
        store.dispatch("changeLoginStatus", false);
      })
      .catch(err => {
        message.error(err);
      });
  },
  // 签约协议
  sign() {
    ajax
      .post(api.sign)
      .then(res => {
        window.location.href = res.data;
      })
      .catch(err => {
        message.error(err);
      });
  }
};
